package com.sky.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 */
@Data
@ApiModel(value = "Result", description = "后端响应的统一格式的结果")
public class Result<T> implements Serializable {
    @ApiModelProperty(value = "编码：1成功，0和其它数字为失败")
    private Integer code; //编码：1成功，0和其它数字为失败
    @ApiModelProperty(value = "错误信息")
    private String msg; //错误信息
    @ApiModelProperty(value = "响应数据")
    private T data; //数据

    @ApiOperation(value = "成功返回结果,无响应数据")
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 1;
        return result;
    }

    @ApiOperation(value = "成功返回结果,带响应数据")
    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = 1;
        return result;
    }

    @ApiOperation(value = "失败返回结果")
    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = 0;
        return result;
    }

}
